package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.service.WechatService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信小程序工具组件接口类
 *
 * @Author Yjr
 * @Date 2025/5/23 16:21
 */
@Service
public class WechatServiceImpl implements WechatService {
    @Value("${wechat.appid}")
    private String appid;

    @Value("${wechat.secret}")
    private String secret;

    private static final String GET_OPEN_ID_URL = "https://api.weixin.qq.com/sns/jscode2session";

    private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";

    private static final String POST_PHONE_NUMBER_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    /**
     * 获取OpenId
     *
     * @param code
     * @return
     */
    @Override
    public String getOpenId(String code) {
        Map<String, Object> map = getParam();
        map.put("js_code", code);
        map.put("grant_type", "authorization_code");

        String resultJson = HttpUtil.get(GET_OPEN_ID_URL, map);
        if (StringUtils.isEmpty(resultJson)) {
            throw new RuntimeException("微信登录失败");
        }

        JSONObject jsonObject = JSON.parseObject(resultJson);
        return jsonObject.getString("openid");
    }

    /**
     * 获取公共参数
     *
     * @return
     */
    private @NotNull Map<String, Object> getParam() {
        Map<String, Object> map = new HashMap<>();
        map.put("appid", appid);
        map.put("secret", secret);
        return map;
    }

    /**
     * 获取手机号
     *
     * @param code
     * @return
     */
    @Override
    public String getPhone(String code) {
        String token = getToken();
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);

        String resultJson = HttpUtil.post(POST_PHONE_NUMBER_URL + token, JSON.toJSONString(map));
        if (StringUtils.isEmpty(resultJson)) {
            throw new RuntimeException("微信登录失败");
        }

        JSONObject jsonObject = JSON.parseObject(resultJson);
        if (jsonObject.getInteger("errcode")!=0) {
            throw new RuntimeException("微信登录失败");
        }
        String phoneInfo = jsonObject.getString("phone_info");
        return JSON.parseObject(phoneInfo).getString("phoneNumber");
    }

    /**
     * 获取token
     *
     * @return
     */
    private String getToken() {
        Map<String, Object> map = getParam();
        map.put("grant_type", "client_credential");

        String resultJson = HttpUtil.get(GET_ACCESS_TOKEN_URL, map);
        if (StringUtils.isEmpty(resultJson)) {
            throw new RuntimeException("微信登录失败");
        }

        JSONObject jsonObject = JSON.parseObject(resultJson);
        return jsonObject.getString("access_token");
    }
}
